package com.woniu.shop.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.woniu.entity.Result;
import com.woniu.entity.ResultCode;
import com.woniu.shop.entity.dto.ShowShopIncomeDTO;
import com.woniu.shop.entity.vo.ShopIncomeVO;
import com.woniu.shop.pojo.ShopIncome;
import com.woniu.shop.service.IShopIncomeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

/**
 * 门店收益表 前端控制器
 *
 * @author LiFangyu
 * @since 2023-02-15
 */
@RestController
@RequestMapping("/shop-income")
@Api(tags = "门店收益控制类")
public class ShopIncomeController {

    @Autowired
    private IShopIncomeService shopIncomeService;

    @PostMapping("/show")
    @ApiOperation("显示门店收益数据")
    public Result<ShopIncomeVO> showShopIncome(@RequestBody ShowShopIncomeDTO showShopIncomeDTO) throws ParseException, JsonProcessingException {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        if(!StringUtils.isEmpty(showShopIncomeDTO.getYear()) && Integer.parseInt(showShopIncomeDTO.getYear()) == calendar.get(Calendar.YEAR)){
            return Result.error(ResultCode.PARAM_IS_INVALID.getCode(), "默认显示本年数据，无需重复查询");
        }
        ShopIncomeVO shopIncomeVO = shopIncomeService.showShopIncome(showShopIncomeDTO);
        return Result.ok(shopIncomeVO);
    }

    @GetMapping("/get/{id}")
    @ApiOperation("查看收益详情|id")
    public Result<ShopIncome> getById(@PathVariable String id){
        ShopIncome shopIncome = shopIncomeService.getById(id);
        return Result.ok(shopIncome);
    }

}
